package com.rongj.generation.core.util;

import java.math.BigDecimal;
import java.util.*;

public class EntityUtils {
    private static Map<String,Class> mapping = new HashMap<String, Class>();

    static {
        mapping.put("INT",Integer.class);
        mapping.put("SMALLINT",Integer.class);
        mapping.put("TINYINT",Integer.class);
        mapping.put("INTEGER",Integer.class);
        mapping.put("BIGINT",Integer.class);
        mapping.put("DOUBLE",Double.class);
        mapping.put("FLOAT",Float.class);
        mapping.put("DECIMAL", BigDecimal.class);

        mapping.put("CHAR", String.class);
        mapping.put("VARCHAR", String.class);

        mapping.put("DATE", Date.class);
        mapping.put("DATETIME", Date.class);
        mapping.put("TIMESTAMP",Long.class);
        mapping.put("TEXT",String.class);

    }

    public EntityUtils(){}

    public static List<EntityDto> analyze(List<String> columnNames,List<String> columnTypes,boolean ishibernarte){
        List<EntityDto> list = new ArrayList<EntityDto>();
        for(int i=0;i<columnNames.size();i++){
            EntityDto dto = new EntityDto();
            dto.setSname(columnNames.get(i));
            String name = getColumnName(columnNames.get(i));
            dto.setName(name);
            if(mapping.get(columnTypes.get(i)) != null){
                dto.setClassTypeName(mapping.get(columnTypes.get(i)).getName());
            }
            dto.setHibernates(ishibernarte);
            list.add(dto);
        }
        return list;
    }

    public static String getColumnName(String name){
        StringBuffer o = new StringBuffer();
        if(name.indexOf("_") != -1){
            String[] strs = name.split("\\_");
            for(int i=0;i<strs.length;i++){
                if(i != 0){
                    o.append(StringUtil.convert(strs[i]));
                }else{
                    o.append(strs[i]);
                }

            }
        }
        if(name.indexOf("-") != -1){
            String[] strs = name.split("\\-");
            for(int i=0;i<strs.length;i++){
                if(i != 0){
                    o.append(StringUtil.convert(strs[i]));
                }else{
                    o.append(strs[i]);
                }
            }
        }
        return name;
    }
}
